/*
 * @Author: hanxin 
 * @Date: 2021-08-11 21:07:22 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-15 23:36:12
 * 客户端与服务端保持链接线程1
 */
package service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import common.Message;
import common.MessageType;

public class clientConnectServerThread extends Thread {
    private Socket socket1 = null;
    private boolean loop = true;

    // 构造器允许传入Socket
    public clientConnectServerThread(Socket socket) {
        this.socket1 = socket;
    }

    public Socket getSocket1() {
        return socket1;
    }

    public void setSocket1(Socket socket1) {
        this.socket1 = socket1;
    }

    public clientConnectServerThread() {

    }

    public void exitSocket() {// 退出登录关闭线程
        loop = false;

    }

    @Override
    public void run() {
        while (loop) {
            try {
                // 客户端等待服务端消息发送
                // 如果服务器没有发送message对象那么线程会阻塞在此位置
                ObjectInputStream ooi = new ObjectInputStream(socket1.getInputStream());
                Message mess = (Message) ooi.readObject();

                // 如果服务端返回的是在线用户列表就读取
                if (mess.getMessType().equals(MessageType.MESSAGE_RET_ONLINE_LIST)) {

                    System.out.println("\n--@48!---current_User_List--@48!---");

                    String onlinelist[] = mess.getContent().split(" ");
                    for (int i = 0; i < onlinelist.length; i++) {
                        System.out.println("User: " + onlinelist[i]);
                    }
                } else if (mess.getMessType().equals(MessageType.MESSAGE_COMM_MES)) {

                    System.out.println("\nFor---" + mess.getSender() + "---Message");
                    System.out.println(mess.getSendTime());
                    System.out.println(mess.getContent());
                } else if (mess.getMessType().equals(MessageType.MESSAGE_COMM_MES_TOALL)) {

                    System.out.println("\nFor---" + mess.getSender() + "---Mass_Message");
                    System.out.println(mess.getContent());
                    System.out.println(mess.getSendTime());

                } else if (mess.getMessType().equals(MessageType.MESSAGE_FILE_MES)) {
                    String saveDic = "fileSave\\";
                    File fileSave = new File(saveDic);

                    if (!fileSave.exists()) {
                        fileSave.mkdir();
                    }
                   //读取服务端发回的文件到指定路径
                    String spl[] = mess.getSrc().split("/");

                    //指定文件名
                    File fullFileSave = new File(fileSave, spl[spl.length - 1]);
                    System.out.println(fullFileSave);
                    //写入文件
                    FileOutputStream fOutputStream = new FileOutputStream(fullFileSave);
                    fOutputStream.write(mess.getData());
                    fOutputStream.close();

                    System.out.println("\nFor----" + mess.getSender() + "----File----Alread_Save_To_fileSave");
                    System.out.println(mess.getSendTime());
                }

            } catch (Exception e) {
              //e.printStackTrace();
              System.out.println("Logout...");// 用户没有正常退出登录就退出程序,客户端异常退出
             
                break;
            }
        }
    }
}
